package com.geng.dao.impl;

import com.geng.dao.BaseAdminDao;
import com.geng.utils.DruidUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDaoMysql extends DruidUtil implements BaseAdminDao {
    private static final String SQL_UPDATE_LOGIN_TIME = "UPDATE EADMIN SET LOGINTIME=?,LOGINIP=? WHERE USERNAME=?";
    private static final String SQl_LOGIN = "SELECT ID FROM EADMIN WHERE USERNAME=? AND PASSWORD=?";
    /**
     * 根据用户名，更新登录时间和登录ip
     *
     * @param username
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        List params = new ArrayList<>();
        try {
            params.add(new java.sql.Date(date.getTime()));
            params.add(ip);
            params.add(username);
            update(SQL_UPDATE_LOGIN_TIME,params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
    }

    /**
     * 管理员根据账号密码登录
     *
     * @param username 账号
     * @param password 密码
     * @return 登录的结果，true表示登录成功
     */
    @Override
    public boolean login(String username, String password) {
        try {
            List params = new ArrayList();
            params.add(username);
            params.add(password);
            resultSet = query(SQl_LOGIN,params);
            return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return false;
    }
}
